using System;
using UnityEngine;

[ExecuteInEditMode]
public class ControllerPipeCorner : ControllerGeneric
{
	private static float unit = 1f;

	public float length = 10f;

	public float height = 6f;

	public float width = 1f;

	public GameObject prisma1;

	public GameObject prisma2;

	public GameObject cub;

	public GameObject prisma3;

	public GameObject prisma4;

	public float unghiRampa;

	private float widthOld;

	private float heightOld;

	private float lengthOld;

	private bool staticState;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			UnityEngine.Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (Application.isEditor && !Application.isPlaying)
		{
			SetupRemoteControl();
			if (IsRemoteControlActive())
			{
				base.transform.position = remoteControl.transform.position;
				base.transform.rotation = remoteControl.transform.rotation;
				length = remoteControl.transform.localScale.z;
				height = remoteControl.transform.localScale.y;
				width = remoteControl.transform.localScale.x;
			}
			if (width < 0.01f)
			{
				width = 0.01f;
			}
			if (length < width)
			{
				length = width;
			}
			if (height < width)
			{
				height = width;
			}
			if (remoteControl != null)
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
			Setup();
			SetupUVs();
		}
	}

	private void Setup()
	{
		AssureParenting();
		SetNames();
		if (widthOld == width && heightOld == height && lengthOld == length)
		{
			return;
		}
		widthOld = width;
		heightOld = height;
		lengthOld = length;
		Vector3 zero = Vector3.zero;
		Vector3 one = Vector3.one;
		Vector3 zero2 = Vector3.zero;
		float num = Mathf.Sqrt((length - width) * (length - width) + (height - width) * (height - width));
		float num2 = Mathf.Acos((length - width) / num) * 57.29578f;
		float num3 = Mathf.Sqrt(num * num + width * width);
		float num4 = Mathf.Acos(num / num3) * 57.29578f;
		zero.z = width + Mathf.Cos((num2 + num4) * ((float)Math.PI / 180f)) * (num3 * 0.5f);
		zero.y = Mathf.Sin((num2 + num4) * ((float)Math.PI / 180f)) * (num3 * 0.5f);
		one.y = num / unit;
		one.z = width / unit;
		one.x = width / unit;
		zero2.x = 0f - num2;
		float num5 = (90f - num2) / 2f;
		float num6 = width * Mathf.Tan(num5 * ((float)Math.PI / 180f));
		float y = width * Mathf.Sin(num5 * 2f * ((float)Math.PI / 180f));
		float z = num6 * Mathf.Sin((180f - (90f - num5) * 2f) * ((float)Math.PI / 180f));
		float num7 = num2 / 2f;
		float num8 = width * Mathf.Tan(num7 * ((float)Math.PI / 180f));
		float num9 = num8 * Mathf.Sin((180f - (90f - num7) * 2f) * ((float)Math.PI / 180f));
		float num10 = num8 + num8 * Mathf.Cos((180f - (90f - num7) * 2f) * ((float)Math.PI / 180f));
		if (cub != null)
		{
			cub.transform.localPosition = zero - new Vector3(0f, height / 2f, length / 2f);
			cub.transform.localEulerAngles = new Vector3(0f, -90f, -90f + num2);
			cub.transform.localScale = one;
		}
		if (prisma1 != null)
		{
			if (num6 == 0f)
			{
				prisma1.SetActive(false);
			}
			else
			{
				prisma1.SetActive(true);
				prisma1.transform.localPosition = Vector3.zero - new Vector3(0f, height / 2f, length / 2f);
				prisma1.transform.localEulerAngles = new Vector3(-90f, 90f, 0f);
				prisma1.transform.localScale = new Vector3(width / unit, width / unit, num6 / unit);
			}
		}
		if (prisma2 != null)
		{
			if (num6 == 0f)
			{
				prisma2.SetActive(false);
			}
			else
			{
				prisma2.SetActive(true);
				prisma2.transform.localPosition = new Vector3(0f, y, z) - new Vector3(0f, height / 2f, length / 2f);
				prisma2.transform.localEulerAngles = new Vector3(num2, 180f, 90f);
				prisma2.transform.localScale = new Vector3(width / unit, width / unit, num6 / unit);
			}
		}
		if (prisma3 != null)
		{
			if (num8 == 0f)
			{
				prisma3.SetActive(false);
			}
			else
			{
				prisma3.SetActive(true);
				prisma3.transform.localPosition = new Vector3(0f, height - num9, length - num10) - new Vector3(0f, height / 2f, length / 2f);
				prisma3.transform.localEulerAngles = new Vector3(0f - num2, 0f, 90f);
				prisma3.transform.localScale = new Vector3(width / unit, width / unit, num8 / unit);
			}
		}
		if (prisma4 != null)
		{
			if (num8 == 0f)
			{
				prisma4.SetActive(false);
				return;
			}
			prisma4.SetActive(true);
			prisma4.transform.localPosition = new Vector3(0f, height, length) - new Vector3(0f, height / 2f, length / 2f);
			prisma4.transform.localEulerAngles = new Vector3(0f, 180f, 90f);
			prisma4.transform.localScale = new Vector3(width / unit, width / unit, num8 / unit);
		}
	}

	private void SetStatic(bool newStaticState = true)
	{
		if (newStaticState != staticState)
		{
			staticState = newStaticState;
			cub.isStatic = staticState;
			prisma1.isStatic = staticState;
			prisma2.isStatic = staticState;
			prisma3.isStatic = staticState;
			prisma4.isStatic = staticState;
		}
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cub.name = "PipeCornerMiddle";
		prisma1.name = "PipeCornerPrism1";
		prisma2.name = "PipeCornerPrism2";
		prisma3.name = "PipeCornerPrism3";
		prisma4.name = "PipeCornerPrism4";
	}

	private void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
			remoteControl.gameObject.AddComponent<PresetsPipeCorner>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		ControllerPipeCorner component = rc.controller.GetComponent<ControllerPipeCorner>();
		unghiRampa = component.unghiRampa;
		PresetsPipeCorner component2 = rc.gameObject.GetComponent<PresetsPipeCorner>();
		PresetsPipeCorner component3 = remoteControl.GetComponent<PresetsPipeCorner>();
		component3.lockHeight = component2.lockHeight;
		component3.lockLenght = component2.lockLenght;
		component3.angle = component2.angle;
	}

	public void SetupUVs()
	{
		SetupUVs_MainPipe();
		SetupUVs_Prism(prisma1);
		SetupUVs_Prism(prisma2);
		SetupUVs_Prism(prisma3);
		SetupUVs_Prism(prisma4);
	}

	private void SetupUVs_MainPipe()
	{
		float num = 0.0625f;
		Mesh mesh = cub.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		Vector3[] normals = mesh.normals;
		Vector3[] vertices = mesh.vertices;
		for (int i = 0; i < vertices.Length; i++)
		{
			Vector3 vector = normals[i];
			Vector3 vector2 = vertices[i];
			if (vector.x != 0f || vector.z != 0f)
			{
				float num2 = Mathf.Sign(vector2.y);
				uv[i].y = num2 * (num * cub.transform.localScale.y / width);
			}
		}
		mesh.uv = uv;
	}

	private void SetupUVs_Prism(GameObject go)
	{
		float num = 0.125f;
		Mesh mesh = go.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		Vector3[] normals = mesh.normals;
		Vector3[] vertices = mesh.vertices;
		for (int i = 0; i < vertices.Length; i++)
		{
			Vector3 vector = normals[i];
			Vector3 vector2 = vertices[i];
			if (vector.x != 0f || vector.y != 0f)
			{
				uv[i].y = num * vector2.z * go.transform.localScale.z / width;
			}
		}
		mesh.uv = uv;
	}
}
